<template>
  <div>
    <van-cell title="搜索历史">
      <div v-if="isDeleteShow">
        <span @click="$emit('clearAllSearch')">全部删除</span>
        &nbsp;&nbsp;
        <span @click="isDeleteShow = false">完成</span>
      </div>
      <van-icon name="delete-o" v-else @click="isDeleteShow = true" />
    </van-cell>
    <van-cell
      v-for="(item, index) in searchHistories"
      :key="index"
      :title="item"
      @click="onSearchHistoryClick(item, index)"
    >
      <van-icon name="close" v-show="isDeleteShow" />
    </van-cell>
  </div>
</template>

<script>
export default {
  name: 'SearchHistory',
  props: {
    searchHistories: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      isDeleteShow: false // 控制删除的显示状态
    }
  },
  methods: {
    onSearchHistoryClick(item, index) {
      if (this.isDeleteShow) {
        // 如果是删除状态
        this.searchHistories.splice(index, 1)
      } else {
        // 如果是非删除状态  点击后 重新触发展示搜索结果
        this.$emit('search', item)
      }
    }
  }
}
</script>

<style lang="less" scoped></style>
